var pate = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
function checkEmail(){
	var val = DWRUtil.getValue("email");
	if(val!=null&&val!=""){
//		var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
		if(val.search(pate)==-1){
			//DWRUtil.setValue("emaildesc","请填写正确的邮箱地址");
			
			checkNotPassed('valid_email_result','请填写正确的邮箱地址');
			return false;
		}
		UserManager.checkEMail(val,function(data){
			if(data!=0){
				//DWRUtil.setValue("valid_email_result","该邮箱已被注册");
				checkNotPassed('valid_email_result','该邮箱已被注册');
				return false;
			}else{
//				DWRUtil.setValue("valid_email_result","可以注册");
				checkPassed('valid_email_result');
				return true;
			}
		});
	}
}
var rePwdBlured = false;
function checkPassword() {
	var p = DWRUtil.getValue("password");
	if(p.length<6 || p.length>20){
		checkNotPassed('valid_password_result','密码长度为6-20位！');
        return false;
    } else {
		checkPassed('valid_password_result');
	}
	if (rePwdBlured) return checkRePassword.call($('password'));
	return true;
}
function checkRePassword() {
	rePwdBlured = true;
	var passwd = DWRUtil.getValue("password");
	var rpass = DWRUtil.getValue("confm");
	if (!passwd) return false;
	if (!rpass) {
		checkNotPassed('valid_repassword_result','确认密码不能为空');
		return false;
	}
	if (rpass != passwd) {
		checkNotPassed('valid_repassword_result','两次输入的密码不一致');
		return false;
	} else {
		checkPassed('valid_repassword_result');
		return true;
	}
}

function checkNickname(){
	if (!DWRUtil.getValue("nickname")) {
		checkNotPassed('valid_nickname_result','昵称不能为空');
		return false;
	} else {
		checkPassed('valid_nickname_result');
		return true;
	}
}
//注册
function register(){
	var email = $("email");
	var nickname = DWRUtil.getValue("nickname");
	var passwd = DWRUtil.getValue("password");
	if(email.value.search(pate)==-1){
		//DWRUtil.setValue("emaildesc","请填写正确的邮箱地址");
		
		checkNotPassed('valid_email_result','请填写正确的邮箱地址');
		email.focus();
		return false;
	}
	if(!checkPassword()){
		return false;
	}
	if(!checkNickname()){
		return false;
	}
	var user = new Object();
	user.email = email.value;
	user.nickName = nickname;
	user.passWord = passwd;
	UserManager.adduser(user,function(data){
		if(data != null){
			window.returnValue = data;
	    	window.close();
		}else{
			alert("添加失败！");
		}
	});
}
//登录
function login(){
	var email = DWRUtil.getValue("email");
    var passwd = DWRUtil.getValue("password");
    UserManager.loginSys(email,passwd,function(data){
    	if(data!=null){
	    	window.returnValue = data;
	    	window.close();
    	}else{
    		checkNotPassed('valid_email_result','用户不存在或密码错误');
    	}
    });
}

window.onload = function initpage(){
	$("email").focus();
}
function checkNotPassed(id,msg) {
	$(id).className="icon not_passed error";
	DWRUtil.setValue(id,msg);
//	$('#'+ id).attr('class','icon not_passed error').html(msg).parent().find('.gray').hide();
}
function checkPassed(id) {
	$(id).className="icon passed";
	DWRUtil.setValue(id," ");
//	$('#' + id).attr('class','icon passed').html('&nbsp;').parent().find('.gray').hide();
}
